package com.sagframe.sagacity.sqltoy.plus;

import com.sagframe.sagacity.sqltoy.plus.conditions.toolkit.FiledNature;
import com.sagframe.sagacity.sqltoy.plus.conditions.toolkit.StringPool;
import com.sagframe.sagacity.sqltoy.plus.multi.MultiContext;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public interface MultiFiledMappingStrategy extends FiledMappingStrategy {

    default String getColumnName(MultiContext context, Class<?> clazz, String filedName) {
        String tableAlisaName = context.getEntityAlisaName(clazz);
        if (tableAlisaName == null || tableAlisaName.isEmpty()) {
            return getColumnName(clazz, filedName);
        }
        return tableAlisaName + StringPool.DOT + getColumnName(clazz, filedName);
    }

    default String getSplitColumnName(MultiContext context, List<FiledNature> natures, String regex) {
        if (natures != null && !natures.isEmpty()) {
            return natures.stream().filter(Objects::nonNull).map(e -> getColumnName(context, e.getClazz(), e.getFiledName())).collect(Collectors.joining(regex));
        }
        return null;
    }

    class DefaultFiledMappingStrategy implements MultiFiledMappingStrategy {

        private DefaultFiledMappingStrategy() {}

        @Override
        public String getColumnName(Class<?> clazz, String filedName) {
            return filedName;
        }

        public static DefaultFiledMappingStrategy getInstance() {
            return DefaultFiledMappingStrategy.DefaultFiledMappingStrategyHolder.instance;
        }

        protected static class DefaultFiledMappingStrategyHolder {
            private static DefaultFiledMappingStrategy instance = new DefaultFiledMappingStrategy();
        }
    }
}
